<?php
namespace app\index\controller;
header('Access-Control-Allow-Origin:*');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

use \think\Session;
use \think\Loader;
use \think\Db;
class Gateway extends Base
{
    public $uid;
    public function index()
    {
      return $this->fetch('index');
    }
    public function bind(){
        $uid = $_POST['uid'];
        Session::set('uid',$uid);
        $client_id = $_POST['client_id'];
        Loader::import('util\Gateway', EXTEND_PATH);
        $gateway = new \util\Gateway();
        if($gateway::isUidOnline($uid)){
            $gateway::closeClient($client_id);
            echo json_encode(array('status'=>1,'info'=>'请不要打开多个页面'));
            exit;
        }
        $gateway::bindUid($client_id,$uid);
        $data =DB::name('homeuser')->field('photo,username,summary')->where(array('id'=>$uid))->find();
        $_SESSION= $data;
        $data['create_time']=date('Y-m-d h:i:s',time());
        $data['msg'] = $_POST['msg'];
        $gateway::sendToAll(json_encode($data));
        $gateway::joinGroup($client_id,1);
        $gateway::setSession($client_id, $_SESSION);
        $group=$gateway::getClientSessionsByGroup(1);
        echo json_encode($group);
    }
     public function message() {
        Loader::import('util\Gateway', EXTEND_PATH);
        $gateway = new \util\Gateway(); 
        $uid = $_POST['uid'];
        $data =DB::name('homeuser')->field('photo,username')->where(array('id'=>$uid))->find();
        $data['create_time']=date('Y-m-d h:i:s',time());
        $data['msg'] = $_POST['msg'];
        if(!empty($_POST['dd'])){
            $data['dd'] =1;
        }
        $gateway::sendToAll(json_encode($data));
    }
    public function close(){
        $uid = $_POST['uid'];
        $client_id = $_POST['client_id'];
        Loader::import('util\Gateway', EXTEND_PATH);
        $gateway = new \util\Gateway(); 
        $group=$gateway::getClientSessionsByGroup(1);
         echo json_encode($group);
    }
    public function online(){
        Loader::import('util\Gateway', EXTEND_PATH);
        $gateway = new \util\Gateway(); 
           $group=$gateway::getClientSessionsByGroup(1);
         echo json_encode($group);     
    }
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('files');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            $filename=$info->getSaveName();
            $result=array(
                'code' => 0,
                'filename' =>$filename
            );
        }else{
            $errorinfo=$file->getError();
            $result=array(
                'code' => 1,
                'errorinfo' =>$errorinfo
            );
        }
        echo json_encode($result);
    }
    public function editupload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('files');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'edit_upload');
        if($info){
            $filename=$info->getSaveName();
            $result=array(
                'code' => 0,
                'filename' =>$filename
            );
        }else{
            $errorinfo=$file->getError();
            $result=array(
                'code' => 1,
                'errorinfo' =>$errorinfo
            );
        }
        echo json_encode($result);
    }
}
